package com.xzuf.core.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.beans.PropertyVetoException;

import javax.swing.JInternalFrame;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdesktop.swingx.action.AbstractActionExt;

import com.xzuf.core.vo.PluginVO;
import com.xzuf.plugin.IModulePlugin;
import com.xzuf.pub.GlobalValue;

/**
 * @author xzgf
 * @since 2008-8-18
 * @version 1.0
 */
public class PluginAction extends AbstractActionExt {
	
	private static final Log logger = LogFactory.getLog(PluginAction.class);
	
	private long startTime = 0;
	
	private PluginVO pluginVO;
	
	public PluginAction(PluginVO pluginVO) {
		setName(pluginVO.getPluginname());
		this.pluginVO = pluginVO;
	}


	/**
	 * {@inheritDoc}
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if(GlobalValue.pluginIsRestart.get(pluginVO.getPlugincode()) == null || GlobalValue.pluginIsRestart.get(pluginVO.getPlugincode()) != null && !GlobalValue.pluginIsRestart.get(pluginVO.getPlugincode()).booleanValue()) {
			JInternalFrame frame = GlobalValue.pluginFrameCache.get(pluginVO.getPlugincode());
			if(frame != null) {
				logger.info("get JInternalFrame from Cache [" + pluginVO.getPlugincode() + "]");
				startTime = System.currentTimeMillis();
				GlobalValue.pluginWindows.removeAll();
				GlobalValue.pluginWindows.updateUI();
				GlobalValue.pluginWindows.add(frame,20);
				frame.setSize(new Dimension(1000,600));
				try {
					frame.setSelected(true);
				} catch (PropertyVetoException e1) {
					e1.printStackTrace();
				}
				try {
					frame.setMaximum(true);
				} catch (PropertyVetoException e1) {
					e1.printStackTrace();
				}
				frame.show();
				logger.info("From cache [" + (System.currentTimeMillis() - startTime) + "]ms");
				return;
			}
		}
		
		try {
			logger.info("get new JInternalFrame ");
			startTime = System.currentTimeMillis();
			IModulePlugin plugin = (IModulePlugin)Class.forName(pluginVO.getExtension().get(0).getRuntime().getRunclass()).newInstance();
			JInternalFrame frame = new JInternalFrame(pluginVO.getPluginname(),true,true,true);
			frame.setSize(new Dimension(1000,600));
			frame.setClosable(true);
			frame.add(plugin.start());
			GlobalValue.pluginWindows.removeAll();
			GlobalValue.pluginWindows.updateUI();
			GlobalValue.pluginWindows.add(frame,20);
			GlobalValue.pluginFrameCache.put(pluginVO.getPlugincode(), frame);
			try {
				frame.setSelected(true);
			} catch (PropertyVetoException e1) {
				e1.printStackTrace();
			}
			try {
				frame.setMaximum(true);
			} catch (PropertyVetoException e1) {
				e1.printStackTrace();
			}
			frame.show();
			logger.info("No cache [" + (System.currentTimeMillis() - startTime) + "]ms");
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}
	}
}
